package controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import data.dao.LeaveDAOException;
import data.dao.daoImplementation.NotFoundException;
import data.dto.LeaveDTO;
import data.dto.UserDTO;

import manager.LeaveManager;

/**
 * Servlet implementation class CancelStatusServlet
 */
@WebServlet("/CancelStatusServlet")
public class CancelStatusServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CancelStatusServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			process(request, response);
		} catch (LeaveDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			process(request, response);
		} catch (LeaveDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void process(HttpServletRequest req, HttpServletResponse res)
			throws LeaveDAOException, SQLException, NotFoundException,
			ServletException, IOException {
		String page = "";
		UserDTO user = util.CurrentUser.getCurrentUser(req.getSession());

		// check user
		if (user == null) {
			page = util.Constants.homePage;
		} else if (util.CurrentUser.isAdmin(user)) {
			page = util.Constants.homePage;
		} else {
			String id = req.getParameter("id");
			String status = req.getParameter("changestatus");

			LeaveManager leaveMgr = new LeaveManager();
			leaveMgr.updateLeaveDetail(Short.parseShort(status),
					Integer.parseInt(id));
			
			page = "ManagePersonalLeaveServlet";
		}

		RequestDispatcher rd = req
				.getRequestDispatcher(page);

		rd.forward(req, res);
	}

}
